<template>
    <div id='building'>
        <el-form ref="loginForm" :model="loginForm" class="loginContainer">
            <h3 class="loginTitle">
                系统登录
            </h3>
            <el-form-item prop="username">
                <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名">
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码">
                </el-input>
            </el-form-item>
            <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
            <el-button type="primary" style="width:100%" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    import {setToken} from "../../utill/Token";
    import {login} from "../../api/api";

    export default {
        name: "Login",
        data() {
            return {
                captchaUrl: "",
                loginForm: {
                    username: "",
                    password: "",

                },
                checked: "",

            }
        },
        mounted() {
            this.logininfo();
            this.keyupSubmit();
        },
        methods: {
            submitLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        login(this.loginForm).then(res => {
                            if (res.data.status === 200) {
                                setToken('admin_token', res.data.data.token);
                                setToken('admin_username', res.data.data.username);
                                setToken('token_type', "admin_token");
                                this.saveUsernameAndPassword()
                                this.$router.push({path: '/auth/index'});
                            }
                        })
                    }
                })
            },
            saveUsernameAndPassword(){
                if(this.checked){
                    this.$cookies.set("username",this.loginForm.username);
                    this.$cookies.set("password",this.loginForm.password);
                    this.$cookies.set("checked","true");
                }else{
                    this.$cookies.set("username","");
                    this.$cookies.set("password","");
                    this.$cookies.set("checked","false");
                }
            },
            logininfo(){
                let username = this.$cookies.get("username");
                let password = this.$cookies.get("password");
                let checked =this.$cookies.get("checked");
                console.log("checked"+checked);
                console.log("username"+username);
                console.log("password"+password);
                if(checked=="true"){
                    console.log("checked"+checked);
                    this.loginForm.username = username;
                    this.loginForm.password = password;
                    this.checked = true;
                }
            },

            // 点击回车键登录
            keyupSubmit() {
                document.onkeydown = (e) => {
                    let _key = window.event.keyCode;
                    if (_key === 13 && !this.clickState) {
                        this.submitLogin();
                    }
                }
            }
        }
    };
</script>

<style lang="scss" scoped>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background-color: rgba(214, 255, 250, 0.6);
        border: 1px solid #e28180;
        box-shadow: 0 0 25px #9eecff;
    }

    .loginTitle {
        margin: 0px auto 48px auto;
        text-align: center;
        font-size: 30px;
    }

    #building {
        background-image: url("../../assets/img/logn_background.png");
        width: 100%;
        height: 100%;
        position: fixed;
        background-size: 100% 100%;
    }

    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
        width: 50%;
    }
</style>

